{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "WBK_EQ2mjh1h"
   },
   "source": [
    "# Data fold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "ILQh5fsg8g_w"
   },
   "outputs": [],
   "source": [
    "index = 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "tjVbXSt9jmwU"
   },
   "source": [
    "# Code & Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "sHV_K3NNp2d8",
    "outputId": "394a956b-5a6a-4f69-a244-15cabf9598a9"
   },
   "outputs": [],
   "source": [
    "from google.colab import drive\n",
    "drive.mount(\"/content/drive\")\n",
    "!rm -rf sample_data\n",
    "!unzip -q drive/MyDrive/GWC21/train.zip -d ./\n",
    "!unzip -q drive/MyDrive/GWC21/test.zip -d ./\n",
    "!cp drive/MyDrive/GWC_data/submission.csv ./\n",
    "!cp drive/MyDrive/GWC_data/{index}.zip ./"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "LAYKIiDIqGYA"
   },
   "outputs": [],
   "source": [
    "!unzip -q {index}.zip -d ./"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "YDCc1hVhqKP7"
   },
   "outputs": [],
   "source": [
    "import shutil\n",
    "\n",
    "with open(f'{index}/val.txt') as f:\n",
    "  for line in f:\n",
    "    file = 'train/' + line.strip()\n",
    "    shutil.copyfile(file, f'{index}/images/val/'+line.strip())\n",
    "with open(f'{index}/train.txt') as f:\n",
    "  for line in f:\n",
    "    file = 'train/' + line.strip()\n",
    "    shutil.copyfile(file, f'{index}/images/train/'+line.strip())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "B_rRAtNB6q05",
    "outputId": "ec5bf2f7-794d-4c1d-bc51-93b955e96bf2"
   },
   "outputs": [],
   "source": [
    "!pip uninstall -y PyYAML\n",
    "!pip install PyYAML==5.3.1\n",
    "!pip install ensemble_boxes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "5f9mXPEd8IRh"
   },
   "outputs": [],
   "source": [
    "import yaml\n",
    "\n",
    "result = {'train': f'../{str(index)}/images/train', 'val': f'../{str(index)}/images/val', 'nc': 1, 'names': ['wheat']}\n",
    "with open('GWC_YOLO/data/custom.yaml', 'w') as f:\n",
    "  dump = yaml.dump(result, default_flow_style=False)\n",
    "  f.write(dump)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "t8r3s6gN6Myq"
   },
   "outputs": [],
   "source": [
    "!cd GWC_YOLO && python train.py --name 4fold{index}_800_45 --img 800 --batch 8 --epochs 45 --data custom.yaml --weights yolov5x.pt --cache-images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "60qH9Ot-lAvo"
   },
   "outputs": [],
   "source": [
    "!cp -r GWC_YOLO/runs/train/4fold3_800_45 drive/MyDrive/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "qRRzaDpmCJAr"
   },
   "source": [
    "# Pseudo labeling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "grMOv3iXxCpk"
   },
   "outputs": [],
   "source": [
    "import os, shutil\n",
    "\n",
    "def run_pseudo_labeling(weights, base_dir, generate=True):\n",
    "  if generate:\n",
    "    if os.path.exists('pseudo'):\n",
    "      shutil.rmtree('pseudo')\n",
    "    os.mkdir('pseudo')\n",
    "    os.mkdir('pseudo/images')\n",
    "    os.mkdir('pseudo/labels')\n",
    "    os.mkdir('pseudo/images/train')\n",
    "    os.mkdir('pseudo/labels/train')\n",
    "    !cd GWC_YOLO && python detect.py --img-size 800 --weights {weights} --source ../test --augment --nosave --save-txt --conf-thres 0.5\n",
    "\n",
    "    files = os.listdir(base_dir)\n",
    "    for file in files:\n",
    "      shutil.copyfile(os.path.join(base_dir, file), 'pseudo/labels/train/'+file)\n",
    "    images = os.listdir('test')\n",
    "    for image in images:\n",
    "      shutil.copyfile(os.path.join('test', image), 'pseudo/images/train/'+image)\n",
    "\n",
    "  result = {'train': [f'../{str(index)}/images/train', '../pseudo/images/train'], 'val': f'../{str(index)}/images/val', 'nc': 1, 'names': ['wheat']}\n",
    "  with open('GWC_YOLO/data/custom_pseudo.yaml', 'w') as f:\n",
    "    dump = yaml.dump(result, default_flow_style=False)\n",
    "    f.write(dump)\n",
    "  !cd GWC_YOLO && python train.py --name 4fold{index}_800_new-wbf-pseudo-final_15_lr_0100 --img 800 --batch 8 --epochs 15 --data custom_pseudo.yaml --weights {weights}  --hyp data/hyp.final.yaml --cache"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "XDQTZL-8x2d1"
   },
   "outputs": [],
   "source": [
    "run_pseudo_labeling('../drive/MyDrive/runs/train/round2/4fold3_800_45_0.773/weights/best.pt', 'GWC_YOLO/runs/detect/4fold3_800_45_0.773-best_conf-0.5_aug-True/labels')\n",
    "# run_pseudo_labeling('../drive/MyDrive/4fold0_35_0.778/weights/best.pt', 'GWC_YOLO/runs/detect/4fold0_35_0.778-best_conf-0.5_aug-True/labels')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "j_7dLuMNAcBL",
    "outputId": "dcb58a8e-aaaf-43ad-9dc4-bdf2e1dc2fc5"
   },
   "outputs": [],
   "source": [
    "!cd GWC_YOLO && python train.py --name 4fold{index}_800_new-wbf-pseudo-final_15_lr_0100 --img 800 --batch 8 --epochs 15 --data custom_pseudo.yaml --weights ../drive/MyDrive/runs/train/round2/4fold3_800_45_0.773/weights/best.pt  --hyp data/hyp.final.yaml --cache"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "A-7SX0QjgAiK"
   },
   "outputs": [],
   "source": [
    "!cp -r GWC_YOLO/runs/train/4fold3_800_new-wbf-pseudo-final_15_lr_0100 drive/MyDrive/runs/train/round2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "m7X54Y7OR7Hs"
   },
   "source": [
    "# Save to submission"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "VXQIaX68LbFQ",
    "outputId": "fd45c352-9a86-4bb7-c7b7-f48d9045ccdd"
   },
   "outputs": [],
   "source": [
    "# !pip install ensemble_boxes\n",
    "!cd GWC_YOLO && python detect.py --img-size 800 --weights ../drive/MyDrive/runs/train/round2/4fold3_800_new-wbf-pseudo-final_15_lr_0100/weights/12.pt --name 12 --source ../test --augment --nosave --save-csv --conf-thres 0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "4NsEzexkYLAa",
    "outputId": "298469c0-75f4-4f78-f6de-c1339108ddc6"
   },
   "outputs": [],
   "source": [
    "!pip install aicrowd-cli\n",
    "API_KEY = \"\" \n",
    "!aicrowd login --api-key $API_KEY"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "cPlJ5xQhYI-t"
   },
   "outputs": [],
   "source": [
    "!aicrowd submission create -c global-wheat-challenge-2021 -f GWC_YOLO/runs/detect/12/submission.csv"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "collapsed_sections": [],
   "machine_shape": "hm",
   "name": "Copy of YOLOv5_czz.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
